﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Globalization;

namespace InventoryManager
{
    public partial class ProductSelectionForm : Form
    {
        private string _connectionString;
        private MySqlConnection _connection;

        private bool _enableWeightState;

        private string _pricePerWeight;
        private string _pricePerItem;

        private bool _isOrder;

        public ProductSelectionForm(string connectionString, bool isOrder)
        {
            InitializeComponent();
            _connectionString = connectionString;
            _enableWeightState = false;
            _isOrder = isOrder;

            LoadProductName();
        }

        public string ProdName
        { 
            get { return ProductComboBox.Items[ProductComboBox.SelectedIndex].ToString(); } 
        }

        public string UnitName
        {
            get
            {
                if (ClassifierRadioButton.Checked)
                    return ClassifierRadioButton.Text;
                else
                    return KgRadioButton.Text;
            }
        }

        public string Classifier
        {
            get { return ClassifierLabel.Text; }
        }

        public string Dimension
        {
            get { return DimensionComboBox.Items[DimensionComboBox.SelectedIndex].ToString(); }
        }

        public string Quantity
        {
            get { return QuantityNumericUpDown.Value.ToString(); }
        }

        public string PricePerUnit
        {
            get { return PriceNumericUpDown.Value.ToString(); }
        }

        public bool UnitSelected
        {
            get { return ClassifierRadioButton.Checked; }
        }

        public string Weight
        {
            get
            {
                if (_enableWeightState)
                {
                    if(PerUnitWeightRadioButton.Checked)
                    {
                        double sumWeight = (double)WeightNumericUpDown.Value * (double)QuantityNumericUpDown.Value;
                        return sumWeight.ToString("N0");
                    }
                    else
                    {
                        double sumWeight = (double)WeightNumericUpDown.Value;
                        return sumWeight.ToString("N0");
                    }
                }
                else
                    return "";
            }
        }

        private void LoadProductName()
        {
            try
            {
                _connection = new MySqlConnection(_connectionString);
                _connection.Open();

                string query = "SELECT DISTINCT Name FROM product";

                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

                DataTable dataTable = new DataTable();
                mySqlDataAdapter.Fill(dataTable);

                //BindingSource bindingSource = new BindingSource();
                //bindingSource.DataSource = dataTable;

                //ProductComboBox.DataSource = bindingSource;
                //ProductComboBox.ValueMember = dataTable.Columns[0].ColumnName.ToString();
                //ProductComboBox.DisplayMember = ProductComboBox.ValueMember;
                foreach (DataRow row in dataTable.Rows)
                    ProductComboBox.Items.Add(row.ItemArray[0].ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }

        private void ProductComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            OKButton.Visible = true;

            try
            {
                string selectedProduct = ProductComboBox.Items[ProductComboBox.SelectedIndex].ToString();

                _connection.Open();

                string query = "SELECT Dimension FROM product WHERE Name = \'" + selectedProduct + "\' ORDER BY Dimension";

                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

                DataTable dataTable = new DataTable();
                mySqlDataAdapter.Fill(dataTable);

                //BindingSource bindingSource = new BindingSource();
                //bindingSource.DataSource = dataTable;

                //DimensionComboBox.DataSource = bindingSource;
                //DimensionComboBox.DisplayMember = dataTable.Columns[0].ColumnName;

                DimensionComboBox.Items.Clear();

                foreach (DataRow row in dataTable.Rows)
                    DimensionComboBox.Items.Add(row.ItemArray[0].ToString());

                DimensionComboBox.SelectedIndex = 0;
                DimensionComboBox_SelectionChangeCommitted(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                _connection.Close();
            }
        }

        private void DimensionComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                string selectedProduct = ProductComboBox.Items[ProductComboBox.SelectedIndex].ToString();
                string selectedDimension = DimensionComboBox.Items[DimensionComboBox.SelectedIndex].ToString();

                if (sender != null)
                    _connection.Open();

                string query = "SELECT Classifier, Weight, PricePerWeight, PricePerItem, ItemsRemain, ItemsRemainVat FROM product WHERE Name = \'" + selectedProduct + "\' " +
                    "AND Dimension = \'" + selectedDimension + "\'";

                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(query, _connection);

                DataTable dataTable = new DataTable();
                mySqlDataAdapter.Fill(dataTable);

                ClassifierLabel.Visible = true;
                ClassifierLabel.Text = dataTable.Rows[0][0].ToString();
                ClassifierRadioButton.Text = dataTable.Rows[0][0].ToString();

                _pricePerWeight = dataTable.Rows[0][2].ToString();
                _pricePerItem = dataTable.Rows[0][3].ToString();

                if (_pricePerWeight == "")
                {
                    ClassifierRadioButton.Enabled = true;
                    KgRadioButton.Enabled = false;
                    ClassifierRadioButton.Checked = true;
                    KgRadioButton.Checked = false;
                    PriceNumericUpDown.Value = Convert.ToDecimal(dataTable.Rows[0][3].ToString());
                }
                else if (_pricePerItem == "")
                {
                    KgRadioButton.Enabled = true;
                    ClassifierRadioButton.Enabled = false;
                    KgRadioButton.Checked = true;
                    ClassifierRadioButton.Checked = false;
                    PriceNumericUpDown.Value = Convert.ToDecimal(dataTable.Rows[0][2].ToString());
                }
                else
                {
                    KgRadioButton.Enabled = true;
                    ClassifierRadioButton.Enabled = true;

                    ChangeProductPrice();
                }

                string weight = dataTable.Rows[0][1].ToString();

                if (weight != "")
                {
                    _enableWeightState = true;
                    WeightLabel.Enabled = true;
                    WeightNumericUpDown.Enabled = true;
                    KgLabel.Enabled = true;
                    PerUnitWeightRadioButton.Enabled = true;
                    TotalWeightRadioButton.Enabled = true;
                    WeightNumericUpDown.Value = Convert.ToDecimal(weight);
                }
                else
                {
                    _enableWeightState = false;
                    WeightLabel.Enabled = false;
                    WeightNumericUpDown.Enabled = false;
                    KgLabel.Enabled = false;
                    PerUnitWeightRadioButton.Enabled = false;
                    PerUnitWeightRadioButton.Checked = true;
                    TotalWeightRadioButton.Enabled = false;
                    WeightNumericUpDown.Value = 0;
                }

                int itemsRemain = Int32.Parse(dataTable.Rows[0][4].ToString());
                int itemsRemainVat = Int32.Parse(dataTable.Rows[0][5].ToString());

                if (_isOrder)
                    QuantityNumericUpDown.Maximum = 100000;
                else if (itemsRemain < itemsRemainVat)
                    QuantityNumericUpDown.Maximum = itemsRemain;
                else
                    QuantityNumericUpDown.Maximum = itemsRemainVat;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (sender != null)
                    _connection.Close();
            }
        }

        private void ClassifierRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            ChangeProductPrice();
        }

        private void KgRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            ChangeProductPrice();
        }

        private void ChangeProductPrice()
        {
            if (KgRadioButton.Checked)
                PriceNumericUpDown.Value = Convert.ToDecimal(_pricePerWeight);
            else
                PriceNumericUpDown.Value = Convert.ToDecimal(_pricePerItem);
        }
    }
}
